package com.yuerlive.rainbow.module.recharge;

import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.common.Constants;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.MyGoldData;
import com.yuerlive.rainbow.entity.OrderInfo;
import com.yuerlive.rainbow.entity.RechargeInfo;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.module.login.LoginActivity;
import com.yuerlive.rainbow.module.login.presenter.LoginContract;
import com.yuerlive.rainbow.module.recharge.adapter.RechargeItemAdapter;
import com.yuerlive.rainbow.module.recharge.core.Pay;
import com.yuerlive.rainbow.module.recharge.core.PayCallback;
import com.yuerlive.rainbow.module.recharge.core.PayHelper;
import com.yuerlive.rainbow.module.recharge.core.alipay.AliPay;
import com.yuerlive.rainbow.module.recharge.core.wxpay.WXPay;
import com.yuerlive.rainbow.module.recharge.presenter.RechargeContract;
import com.yuerlive.rainbow.module.recharge.presenter.RechargePresenter;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public class RechargeActivity extends BaseActivity implements RechargeContract.View, PayCallback {
    @BindView(R.id.rvRecharge)
    RecyclerView rvRecharge;
    @BindView(R.id.tvCoin)
    TextView tvCoin;
    @BindView(R.id.imgAli)
    ImageView imgAli;
    @BindView(R.id.imgWx)
    ImageView imgWx;
    @BindView(R.id.tvFirst)
    TextView tvFirst;

    @BindView(R.id.loadingView)
    ContentLoadingProgressBar loadingView;

    GridLayoutManager layoutManager;
    List<RechargeInfo> mDatas = new ArrayList<>();
    RechargeItemAdapter adapter;
    private String title = "我的糖果";
    RechargeContract.Presenter presenter;
    private PayType payType = PayType.WXPAY;
    private int index = 0;
    private int isFirst;

    private OrderInfo payInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void initViews() {
        presenter = new RechargePresenter(this);
        presenter.getMyGold();
        setTitle(title);
        layoutManager = new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        rvRecharge.setLayoutManager(layoutManager);
        adapter = new RechargeItemAdapter(this, mDatas, 0);
        rvRecharge.setAdapter(adapter);
        adapter.setOnItemClickListener((v, p) -> {
            for (int i = 0; i < rvRecharge.getChildCount(); i++) {
                View itemView = rvRecharge.getChildAt(i);
                itemView.setBackgroundResource(R.drawable.recharge_unselected);
                if (isFirst == 0) {
                    tvFirst.setVisibility(View.GONE);
                } else {
                    tvFirst.setVisibility(View.VISIBLE);
                }
                tvFirst.setText(Html.fromHtml(getResources().getString(R.string.format_first_recharge, "" + mDatas.get(p).getFirstChargeValue())));
            }
            index = p;
        });
        User user = UserProxy.getUser();
        if (user == null) {
//            tvCoin.setText(String.valueOf(user.getYuerCoin()));
            jumpToActivity(LoginActivity.class);
        }

        PayHelper.getInstance().addCallback(this);
        PayHelper.getInstance().setActivity(this);
    }


    @OnClick({R.id.imgWx, R.id.imgAli, R.id.btnPay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imgWx:
                payType = PayType.WXPAY;
                imgWx.setImageResource(R.drawable.icon_selected);
                imgAli.setImageResource(R.drawable.icon_unselected);
                break;
            case R.id.imgAli:
                payType = PayType.ALIPAY;
                imgWx.setImageResource(R.drawable.icon_unselected);
                imgAli.setImageResource(R.drawable.icon_selected);
                break;
            case R.id.btnPay:
                if (index < 0) {
                    showSnackbar(getRootView(this), "请选择充值数量", 0);
                    return;
                }
                doPay(payType, index);
                loadingView.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void doPay(PayType payType, int index) {
        switch (payType) {
            case WXPAY:
                presenter.orderPay(String.valueOf(mDatas.get(index).getId()), 2);
                break;
            case ALIPAY:
                presenter.orderPay(String.valueOf(mDatas.get(index).getId()), 1);
                break;
        }
    }

    @Override
    public void setPresenter(RechargeContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showRechargeItem(MyGoldData result) {
        if (result != null && result.getSweetList() != null) {
            isFirst = result.getIsFirst();
            if (isFirst == 0) {
                tvFirst.setVisibility(View.GONE);
            } else {
                tvFirst.setVisibility(View.VISIBLE);
            }

            tvCoin.setText(result.getGolds() + "");
            mDatas.clear();
            mDatas.addAll(result.getSweetList());
            adapter.setIsFirst(result.getIsFirst());
            adapter.notifyDataSetChanged();
            tvFirst.setText(Html.fromHtml(getResources().getString(R.string.format_first_recharge, "" + mDatas.get(0).getFirstChargeValue())));
        }
    }

    @Override
    public void showOrderInfo(BaseData<OrderInfo> orderInfoBaseData) {
        if (orderInfoBaseData.getCode() == 0) {
            payInfo = orderInfoBaseData.getObject();
            switch (payType) {
                case WXPAY:
                    PayHelper.getInstance().pay(WXPay.
                                    class, payInfo.getPrepayId(), Constants.PAY_DISCRIBE,
                            mDatas.get(index).getValue() + "彩虹糖",
                            mDatas.get(index).getRmb() + "");
                     /*   PayHelper.getInstance().pay(WXPay.class, payInfo.getPrepayId(), Constants.PAY_DISCRIBE,
                                mDatas.get(index).getValue() + "彩虹糖",
                                1 + "");*/

                    break;
                case ALIPAY:
                    PayHelper.getInstance().pay(AliPay.class, payInfo.getOutTradeNo(), Constants.PAY_DISCRIBE,
                            mDatas.get(index).getValue()+ "彩虹糖",
                            mDatas.get(index).getRmb() + "");
                    /*PayHelper.getInstance().pay(AliPay.class, payInfo.getOutTradeNo(), Constants.PAY_DISCRIBE,
                            mDatas.get(index).getValue() + "彩虹糖",
                            0.01 + "");*/
                    break;
            }
        }
    }

    @Override
    public void onCallback(int code) {
        if (code == 0) {
            showSnackbar(getRootView(mActivity), "充值成功", 0);
            RechargeInfo info = mDatas.get(index);
            User user = UserProxy.getUser();
            UserInfo userInfo = user.getUserInfo();
            if (isFirst == 0) {
                userInfo.setRainbowSweet(userInfo.getRainbowSweet() + info.getValue());
            } else {
                userInfo.setRainbowSweet(userInfo.getRainbowSweet() + info.getValue() + info.getFirstChargeValue());
                adapter.setIsFirst(0);
            }
            user.setUserInfo(userInfo);
            UserProxy.setUser(user);
            tvCoin.setText(userInfo.getRainbowSweet() + "");
            isFirst = 0;
            adapter.notifyDataSetChanged();
            if (payInfo!=null){
//                presenter.payNotify(payInfo.getOutTradeNo(),payInfo.getOrderId());
            }

        } else {
            showSnackbar(getRootView(mActivity), "充值失败,请稍后再试", 0);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        loadingView.setVisibility(View.GONE);
    }

    enum PayType {
        WXPAY,
        ALIPAY
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PayHelper.getInstance().removeCallback(this);
        PayHelper.getInstance().releaseActivity();
    }
}
